package com.ccb.sc.member.controller;

import com.google.common.collect.Lists;
import com.ccb.sc.auth.TokenAuth;
import com.ccb.sc.auth.TokenModel;
import com.ccb.sc.member.convert.AppMemberConvert;
import com.ccb.sc.member.service.AppMemberMedalService;
import com.ccb.sc.member.service.AppMemberPointsLogService;
import com.ccb.sc.member.service.AppMemberService;
import com.ccb.sc.member.service.SignInService;
import com.ccb.sc.member.vo.AppMemberUpdateVo;
import com.ccb.sc.member.vo.AppMemberVo;
import com.ccb.sc.member.vo.MemberMedalListVo;
import com.ccb.sc.member.vo.MemberPointsLogVo;
import com.ccb.sc.common.bind.annotation.CurrentUser;
//psl import com.ccb.sc.common.bind.exception.BizException;
import com.ccb.sc.common.bind.page.PageParams;
import com.ccb.sc.common.bind.page.PageVo;
import com.ccb.sc.common.bind.response.ResBody;
import com.ccb.sc.common.code.BizCodeFace;
import com.ccb.sc.common.code.ErrorCode;
import com.ccb.sc.common.modules.common.entity.District;
import com.ccb.sc.common.modules.common.service.IDistrictService;
import com.ccb.sc.common.modules.member.entity.Member;
import com.ccb.sc.common.modules.setting.service.ISettingsService;
import com.ccb.sc.common.modules.setting.vo.PlatformSettingsVo;
import com.ccb.sc.common.modules.shopkeeper.entity.Shopkeeper;
import com.ccb.sc.common.modules.shopkeeper.service.ShopkeeperPointsLogService;
import com.ccb.sc.common.modules.shopkeeper.service.ShopkeeperService;
import com.ccb.sc.common.modules.shopkeeper.service.ShopkeeperWithdrawService;
import com.ccb.sc.common.modules.shopkeeper.vo.ShopkeeperPointsLogVo;
import com.ccb.sc.common.modules.shopkeeper.vo.ShopkeeperWithdrawListVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.util.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;

/**
 * @author ken
 * @version 1.0
 * @date 2020-09-16
 */
@RestController
@RequestMapping("/app/member")
@Api(value = "个人中心", tags = "个人中心")
public class AppMemberController {

    private AppMemberService memberService;
    private IDistrictService districtService;
    private SignInService signInService;
    private AppMemberPointsLogService pointsLogService;
    private AppMemberMedalService appMemberMedalService;
    private ShopkeeperService shopkeeperService;
    private ShopkeeperPointsLogService shopkeeperPointsLogService;
    private ShopkeeperWithdrawService withdrawService;
    private ISettingsService settingsService;

    @TokenAuth
    @GetMapping("/info")
    @ApiOperation(value = "个人信息", response = AppMemberVo.class)
    public ResBody<?> info(@ApiIgnore @CurrentUser TokenModel token) {
        Member member = this.memberService.getById(token.getId());
        AppMemberVo vo = AppMemberConvert.INSTANCE.entityToVo(member);
        long registerDays = LocalDate.now().toEpochDay() - member.getCreateTime().toLocalDate().toEpochDay() + 1;
        vo.setRegisterDays(registerDays);
        if (member.getRegionId() > 0) {
            District district = this.districtService.getById(member.getRegionId());
            if (district != null) {
                vo.setRegion(district.getMergeName());
            }
        }
        boolean signIn = this.signInService.isSignIn(token.getId());
        vo.setIsSignIn(signIn);
        Shopkeeper shopkeeper = this.shopkeeperService.findByMemberId(member.getId());
        if (shopkeeper != null) {
            vo.setBalance(shopkeeper.getPoints());
            vo.setTotalBalance(shopkeeper.getTotalPoints());
        }
        return ResBody.success(vo);
    }

    @TokenAuth
    @PutMapping("/info")
    @ApiOperation("/修改个人信息")
    public ResBody<?> changeAvatar(@ApiIgnore @CurrentUser TokenModel token, @RequestBody AppMemberUpdateVo updateVo) {
        boolean result = this.memberService.updateInfo(token.getId(), updateVo);
        if (!result) {
            //psl throw new BizException(BizCodeFace.createBizCode(ErrorCode.FAIL));
        }
        return ResBody.success();
    }

    @TokenAuth
    @GetMapping("/points/list")
    @ApiOperation(value ="积分记录列表", response = MemberPointsLogVo.class, responseContainer = "list")
    public ResBody<?> pointsList(@ApiIgnore @CurrentUser TokenModel token, PageParams queryParam) {
        PageVo<MemberPointsLogVo> result = this.pointsLogService.searchByQueryParam(token.getId(), queryParam);
        return ResBody.success(result);
    }

    @GetMapping("/medal/list")
    @ApiOperation(value ="勋章列表", response = MemberMedalListVo.class, responseContainer = "list")
    public ResBody<?> medalList(@ApiIgnore @CurrentUser TokenModel token) {
        Long memberId = token != null ? token.getId() : null;
        List<MemberMedalListVo> result = this.appMemberMedalService.findMemberMedalList(memberId);
        return ResBody.success(result);
    }

    /*@TokenAuth
    @PostMapping("/medal/{value}")
    @ApiOperation(value ="勋章状态修改")
    public ResBody<?> medalStatus(@ApiIgnore @CurrentUser TokenModel token, @PathVariable Integer value) {
        this.appMemberMedalService.medalStatus(token.getId(), value);
        return ResBody.success();
    }*/


    @GetMapping("/balance/list")
    @ApiOperation(value ="余额记录明细", response = ShopkeeperPointsLogVo.class, responseContainer = "list")
    public ResBody<?> pointsList(PageParams queryParam, @ApiIgnore @CurrentUser TokenModel token) {
        Shopkeeper shopkeeper = this.shopkeeperService.findByMemberId(token.getId());
        if (shopkeeper == null) {
            this.shopkeeperService.create(token.getId());
            PageVo<ShopkeeperPointsLogVo> result = PageVo.create(queryParam.getCurrent(), queryParam.getPageSize(), 0, Lists.newArrayList());
            return ResBody.success(result);
        }
        PageVo<ShopkeeperPointsLogVo> result = this.shopkeeperPointsLogService.searchByQueryParam(shopkeeper.getId(), queryParam);
        return ResBody.success(result);
    }

    @GetMapping("/setting/withdrew")
    @ApiOperation(value = "提现规则", response = String.class)
    public ResBody<?> withdrew() {
        PlatformSettingsVo vo = this.settingsService.getSettings(PlatformSettingsVo.class);
        return ResBody.success(vo.getPointsRule());
    }

    @PostMapping("/withdraw")
    @ApiOperation(value ="申请提现")
    public ResBody<?> withdraw(BigDecimal amount, @ApiIgnore @CurrentUser TokenModel token) {
        Shopkeeper shopkeeper = this.shopkeeperService.findByMemberId(token.getId());
        Assert.notNull(shopkeeper, "无权限，请先申请团长！");
        this.withdrawService.withdraw(shopkeeper.getId(), amount);
        return ResBody.success();
    }

    @GetMapping("/withdraw")
    @ApiOperation(value ="提现记录明细", notes = "提现记录明细", response = ShopkeeperWithdrawListVo.class, responseContainer = "list")
    public ResBody<?> withdraw(PageParams queryParam, @ApiIgnore @CurrentUser TokenModel token) {
        Shopkeeper shopkeeper = this.shopkeeperService.findByMemberId(token.getId());
        Assert.notNull(shopkeeper, "无权限，请先申请团长！");
        PageVo<ShopkeeperWithdrawListVo> result = this.withdrawService.searchByQueryParam(shopkeeper.getId(), queryParam);
        return ResBody.success(result);
    }


    @Autowired
    public void setMemberService(AppMemberService memberService) {
        this.memberService = memberService;
    }

    @Autowired
    public void setDistrictService(IDistrictService districtService) {
        this.districtService = districtService;
    }

    @Autowired
    public void setSignInService(SignInService signInService) {
        this.signInService = signInService;
    }

    @Autowired
    public void setPointsLogService(AppMemberPointsLogService pointsLogService) {
        this.pointsLogService = pointsLogService;
    }

    @Autowired
    public void setMemberMedalService(AppMemberMedalService appMemberMedalService) {
        this.appMemberMedalService = appMemberMedalService;
    }

    @Autowired
    public void setShopkeeperService(ShopkeeperService shopkeeperService) {
        this.shopkeeperService = shopkeeperService;
    }

    @Autowired
    public void setPointsLogService(ShopkeeperPointsLogService shopkeeperPointsLogService) {
        this.shopkeeperPointsLogService = shopkeeperPointsLogService;
    }

    @Autowired
    public void setWithdrawService(ShopkeeperWithdrawService withdrawService) {
        this.withdrawService = withdrawService;
    }

    @Autowired
    public void setSettingsService(ISettingsService settingsService) {
        this.settingsService = settingsService;
    }


}
